package com.na.tools.ping;

public interface NetworkChecker {
	/**
	 * connect the host
	 * @param host
	 * @return connect time(millisecond)
	 */
	public long connect(String host);
	
	public static abstract class BatchNetworkChecker implements NetworkChecker {
		public Host[] batchConnect(String[] hosts) {
			if (hosts == null || hosts.length == 0) {
				return new Host[0];
			}
			Host[] h = new Host[hosts.length];
			for (int i = 0; i < hosts.length; i++) {
				System.out.println("try connect " + hosts[i]);
				h[i] = new Host(hosts[i], connect(hosts[i]));
			}
			return h;
		}
	}
	
	public static class Host implements Comparable<Host> {
		public String host;
		public long time;
		public Host(String host, long time) {
			this.host = host;
			this.time = time;
		}
		@Override
		public int compareTo(Host o) {
			if (time > o.time) return 1;
			if (time < o.time) return -1;
			return 0;
		}
		@Override
		public String toString() {
			return "[host=" + host + ", time=" + time + "]";
		}
	}
	
}
